package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Charge;
import cn.wolfcode.crm.qo.PageResult;
import cn.wolfcode.crm.qo.QueryObject;
import cn.wolfcode.crm.service.IChargeService;
import cn.wolfcode.crm.util.JSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("charge")
public class ChargeController {
    @Autowired
    private IChargeService chargeService;

    @RequestMapping("view")
    public String list(){
        return "/charge/list";
    }

    //异步加载数据
    @RequestMapping("query")
    @ResponseBody
    public Object list(@ModelAttribute("qo") QueryObject qo, Model model) {
        //将数据转换成json格式
        PageResult result = chargeService.queryForList(qo);

        return result;
    }
    //异步加载数据
    @RequestMapping("queryByCard")
    @ResponseBody
    public Object queryByCard(String cardNumber) {
        //将数据转换成json格式
        List<Charge> result = chargeService.queryByCard(cardNumber);

        return result;
    }


    @RequestMapping("insert")
    @ResponseBody
    public Object saveOrUpdate(Charge entity,Long memberId){
        JSONResult result = new JSONResult();
        try {
          chargeService.save(entity,memberId);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark1(e.getMessage());
        }
        return result;
    }


}
